import { Scheme } from '../type/scheme'
import dailyNote from '../api/mys/genshin/dailyNote'
import { ApiVariable, UserVariable, Variable } from '../type/variable'
import { Node } from '../type/node'
/**
 * 将方案里data相关数据解析成具体值，具体见：https://hithac.yuque.com/org-wiki-hithac-tq4vg0/un4p0w/co7dtpd49hi8xgmi?singleDoc# 《设计方案json格式说明》
 * @param scheme
 * @returns
 */
export default function schemeParser(scheme:Scheme):Scheme{
 //todo
  if(dataCheck(scheme)){
    scheme = apiVariableParser(scheme)??null
    scheme = userVariableParser(scheme)??null
    return scheme
  }else return null
}

/**
 * 解析所有的api变量
 * @param scheme
 * @returns
 */
function apiVariableParser(scheme:Scheme):Scheme{
  //todo
  let apiObj:object= {
    "current_resin": 80,
    "max_resin": 160
  };
  let test = 1
  scheme.data.forEach((value,key)=>{
    if(apiObj.hasOwnProperty(key.split(".").pop())){
      value.value = apiObj[key.split(".").pop()]
    }else test = 0
  })
  if(test==0)return null
  return scheme
}

/**
 * 解析所有的用户自定义变量
 * @param scheme
 * @returns
 */
function userVariableParser(scheme:Scheme):Scheme{
  //todo
  let nodes:Map<String,Node>
  //创建节点map
  scheme.data.forEach((value,key)=>{
    if(key[0]=="@"){
      if(value.type=="String"||value.type=="Boolean"){
        value.value=(value as UserVariable).expression
      }else if(value.type=="Number"){
        let node = new Node()
        node.uvariable = value
        nodes.set(key,node)
      }
    }else if(key[0]=="$"){
      if(value.type=="Number"){
        let node = new Node()
        node.uvariable = value
        nodes.set(key,node)
      }
    }
  })
  //解析expression
  nodes = expressionParser(nodes)
  //拓扑排序
  nodes = topoSort(nodes)??null
  //填写data内的value
  let nodesArray = Array.from(nodes)
  let format:string
  for(let i=nodesArray.length;i>=0;i--){
    if(nodesArray[i][0][0]=="$"){ continue }
    else if(nodesArray[i][0][0]=="@"){
      nodesArray[i][1].reqNums.forEach((value,key)=>{
        format=(nodesArray[i][1].uvariable as UserVariable).expression.replace(key,value.toString())
      })
      scheme.data.get(nodesArray[i][0]).value=eval(format)
    }else return null
  }
  return scheme
}

/**
 * 解析所有expression,填入reqs,度和值
 */
function expressionParser(nodes:Map<String,Node>):Map<String,Node>{
  const userRegex = /[@$][a-zA-Z][a-zA-Z0-9_.]{1,}/g
  let matches = []
  nodes.forEach((node,key)=>{
    if(key[0]=="@"){
      matches = (node.uvariable as UserVariable).expression.match(userRegex)
      matches.forEach((nodeName)=>{
        node.reqs.push(nodes.get(nodeName))
        node.reqNums.set(nodeName,nodes.get(nodeName).uvariable.value)
        nodes.get(nodeName).inDegree ++
      })
    }
  })
  return nodes
}

/**
 * 拓扑排序
 */
function topoSort(nodes:Map<String,Node>):Map<String,Node>{
  let sorts :Map<String,Node>
  let i: number = 0
  for(i=0; i<nodes.size; i++){
    nodes.forEach((node,key)=>{
    if(node.inDegree==0){
      sorts.set(key,node)
      node.reqs.forEach((node)=>{
        node.inDegree --
      })
      return;
    }
    })
  }
  nodes.forEach((node,key)=>{
    if(node.inDegree!=0){
      i = -1
    }
  })
  if(i==-1){return null}
  return sorts
}

/**
 * 数据格式检查
 */
function dataCheck(scheme:Scheme):boolean{
  const apiRegex = /\$[a-zA-Z0-9_\.]{1,}/g
  const userRegex = /@[a-zA-Z][a-zA-Z0-9_]{1,}/g
  let a:boolean=false
  scheme.data.forEach((value,key)=>{
    if(key[0]=="$"){
      if(apiRegex.test(key as string)){
        a=true
      }else{
        return false
      }
    }else if(key[0]=="@"){
      if(userRegex.test(key as string)){
        a=true
      }else{
        return false
      }
    }else{
      return false
    }
  })
  return a
}